package com.liang.three;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;
import utils.RabbitmqUtil;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.concurrent.TimeoutException;

/**
 * Created with IntelliJ IDEA.
 * User: XIAOLIANG
 * Date: 2021/6/17
 * Time: 19:10
 * Description: 消息手动应答 消费比较慢 10S 消费一个
 */
public class Work04 {

    public static final String QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws IOException, TimeoutException {


        Channel channel = RabbitmqUtil.getChannel();

        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {

                try {
                    Thread.sleep(30*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println("消费者c2 消费了"+ new String(message.getBody(), StandardCharsets.UTF_8));

                channel.basicAck(message.getEnvelope().getDeliveryTag(),false);

            }
        };

        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {
                System.out.println("消费者c2 取消了 消费"+consumerTag);
            }
        };
//        channel.basicQos(1);
        channel.basicQos(5);////表示 预取值 5条  预期值表示消息堆积在信道里 会堆积5条
        channel.basicConsume(QUEUE_NAME,false,deliverCallback,cancelCallback);


    }

}